<?php
// +----------------------------------------------------------------------
// | astp [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author 荒年 < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 场景二维码
// +----------------------------------------------------------------------
namespace app\wewhat\controller;

use think\Controller;
use app\wewhat\service\HttpRequest;
use app\wewhat\service\Accesstoken;

class Qrcode extends Controller
{
  public function createQrcode($flag = 0,$id = 1)
  {
    $http = new HttpRequest();
    $access = new Accesstoken();
    $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access->getAccessToken();
    if($flag == 0){
      //临时二维码
      $data = '{"expire_seconds": 2592000, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$id.'}}}';
    } else {
      //永久二维吗
      $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$id.'}}}';
    }
    # 得到ticket
    $json = $http->http_request($url,$data);
    $arr = json_decode($json,true);
    $ticket = $arr['ticket'];

    # 用ticket换取二维吗
    $url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket);
    $img = $http->http_request($url);
    file_put_contents('qrcode.jpg',$img);
    return '<img src="http://localhost:8080/qrcode.jpg" alt="">';
  }
}
?>